package com.yanwq.sayhi.controller.club.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yanwq.sayhi.R;
import com.yanwq.sayhi.base.BaseActivity;
import com.yanwq.sayhi.common.client.MImageLoader;
import com.yanwq.sayhi.common.client.OkJsonObjectResponseCallback;
import com.yanwq.sayhi.common.client.OkRequestParams;
import com.yanwq.sayhi.common.client.OkRestClient;
import com.yanwq.sayhi.dao.bean.ClubVip;
import com.yanwq.sayhi.widget.SelectDialog;
import com.yanwq.sayhi.widget.TAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：闫伟强。
 * 日期：2016/6/19。
 */
public class ClubVipAdapter extends TAdapter<ClubVip> {
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        final Context context = viewGroup.getContext();
        ViewHolder holder;

        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_club_vip, viewGroup, false);
            holder.avatar = (ImageView) view.findViewById(R.id.cv_avatar);
            holder.name = (TextView) view.findViewById(R.id.cv_name);
            holder.position = (TextView) view.findViewById(R.id.cv_position);
            holder.operate = (Button) view.findViewById(R.id.cv_operate);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        final ClubVip vip = getItem(i);

        MImageLoader.displayImage(vip.getUser().getAvatarThumbUrl(), holder.avatar);
        holder.name.setText(vip.getName());
        holder.position.setText(getPositionName(i, context, holder.operate, vip));

        return view;
    }

    private String getPositionName(final int position, final Context context, Button button, final ClubVip vip) {
        button.setVisibility(View.VISIBLE);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                removeClubVip(context, vip);
            }
        });

        switch (vip.getPosition()) {
            case 0:
                button.setText("处理");
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        final List<SelectDialog.Item<Integer>> map = new ArrayList<>();
                        map.add(new SelectDialog.Item<>("允许", 0));
                        map.add(new SelectDialog.Item<>("滚粗吧", 1));
                        SelectDialog<Integer> dialog = new SelectDialog<>(context, map);
                        dialog.setOnSelectedListener(new SelectDialog.OnSelectedListener<Integer>() {
                            @Override
                            public void onSelected(String item, Integer integer) {
                                if (integer == 0) {
                                    passClubVip(position, context, vip);
                                } else {
                                    refuseClubVip(position, context, vip);
                                }
                            }
                        });
                        dialog.show();
                    }
                });
                return "申请加入";
            case 1:
                button.setText("滚粗");
                return "已被拒绝";
            case 2:
                button.setVisibility(View.GONE);
                return "boss";
            case 3:
                button.setText("滚粗");
                return "经理";
            case 4:
                button.setText("滚粗");
                return "会员";
        }

        return "";
    }

    private void removeClubVip(final Context context, final ClubVip clubVip) {
        OkRequestParams params = new OkRequestParams();
        params.put("userId", clubVip.getUser().getId())
                .put("clubId", clubVip.getClub().getId());

        OkRestClient.getInstance().post(context, "/clubVip/remove", params, new OkJsonObjectResponseCallback() {
            @Override
            public void onStart() {
                ((BaseActivity) context).showProgressDialog("正在让他滚粗");
            }

            @Override
            public void onSuccess(JSONObject jsonObject) {
                mList.remove(clubVip);
                notifyDataSetChanged();
            }

            @Override
            public void onStop() {
                ((BaseActivity) context).cancelProgressDialog();
            }
        });
    }

    private void passClubVip(final int position, final Context context, final ClubVip clubVip) {
        OkRestClient.getInstance().post(context, "/clubVip/pass", clubVip, new OkJsonObjectResponseCallback() {
            @Override
            public void onStart() {
                ((BaseActivity) context).showProgressDialog("正在加入");
            }

            @Override
            public void onSuccess(JSONObject jsonObject) {
                ClubVip result = JSON.toJavaObject(jsonObject, ClubVip.class);
                mList.set(position, result);
                notifyDataSetChanged();
            }

            @Override
            public void onStop() {
                ((BaseActivity) context).cancelProgressDialog();
            }
        });
    }


    private void refuseClubVip(final int position, final Context context, final ClubVip clubVip) {
        OkRestClient.getInstance().post(context, "/clubVip/refuse", clubVip, new OkJsonObjectResponseCallback() {
            @Override
            public void onStart() {
                ((BaseActivity) context).showProgressDialog("正在拒绝");
            }

            @Override
            public void onSuccess(JSONObject jsonObject) {
                ClubVip result = JSON.toJavaObject(jsonObject, ClubVip.class);
                mList.set(position, result);
                notifyDataSetChanged();
            }

            @Override
            public void onStop() {
                ((BaseActivity) context).cancelProgressDialog();
            }
        });
    }

    private class ViewHolder {
        ImageView avatar;
        TextView name;
        TextView position;
        Button operate;
    }
}
